﻿using GPS.MODELS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPS.DataAccessLayer;
using AutoMapper;
using PagedList;
using GPS.ENTITYS;
using GPS.COMMON;
using System.IO;
using DHTMLX.Scheduler;
using DHTMLX.Scheduler.Controls;
using DHTMLX.Common;
using DHTMLX.Scheduler.Data;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace GPS.Controllers.GPS_XepLich
{
    public class GPS_XepLichController : FDBController
    {
        //
        // GET: /GPS_XepLich/

        DbContextHelper<GPS_NVContext> _db = Singleton<DbContextHelper<GPS_NVContext>>.Inst;
        public ActionResult Index(ViewModelGPS_Sched_DKH SearchModel)
        {
            this.initialCategorySearchAction(SearchModel);

            var entities = _db.Filter<DKH>(o => (o.KH_MA.StartsWith(SearchModel.pKH_MA))
                                                          );

            var model = new List<DKH>();
            foreach (var currentEntity in entities)
            {
                DKH _model = new DKH();
                GPS.COMMON.Helpers.CopyObject<DKH>(currentEntity, ref _model);
                model.Add(_model);
            }

            var pageIndex = SearchModel.Page ?? 1;
            SearchModel.SearchResults = model.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);

            ////Thông kê thông tin học viên và hạng
            //var sumSanLuongs = _db.GetTable<DHV>().Where(o => o.HV_KH_MA == SearchModel.XEPLICH_KH_MA)
            //         .GroupBy(g => g.HV_HANGDT_MA)
            //         .Select(r => new
            //         {
            //             HV_HANGDT_MA = r.Key,
            //             XEPLICH_NUMBER_HOCVIEN = r.Count()
            //         }).ToDictionary(o => o.HV_HANGDT_MA);// hạng làm key

            //if (sumSanLuongs != null && sumSanLuongs.Count > 0)
            //{
            //    SearchModel.GroupByHocVien = new Dictionary<string, int>();

            //    for (int i = 0; i < sumSanLuongs.Keys.Count; i++)
            //    {
            //        SearchModel.GroupByHocVien.Add(sumSanLuongs.Keys.ToList()[i], sumSanLuongs[sumSanLuongs.Keys.ToList()[i]].XEPLICH_NUMBER_HOCVIEN);
            //    }
            //}


            return View(SearchModel);
        }

        public ActionResult GPS_DKH_LOP(string id1)
        {
            //Thông kê thông tin học viên và hạng
            var sumHocviens = _db.GetTable<DHV>().Where(o => o.HV_KH_MA == id1)
                     .GroupBy(g => g.HV_HANGDT_MA)
                     .Select(r => new
                     {
                         HV_HANGDT_MA = r.Key,
                         XEPLICH_NUMBER_HOCVIEN = r.Count()
                     }).ToDictionary(o => o.HV_HANGDT_MA);// hạng làm key

            CrudModelDKH_LOP model = new CrudModelDKH_LOP();
            if (sumHocviens != null && sumHocviens.Count > 0)
            {
                Dictionary<string, int> GroupByHocVien = new Dictionary<string, int>();

                for (int i = 0; i < sumHocviens.Keys.Count; i++)
                {
                    GroupByHocVien.Add(sumHocviens.Keys.ToList()[i], sumHocviens[sumHocviens.Keys.ToList()[i]].XEPLICH_NUMBER_HOCVIEN);
                }
                model.GroupByHocVien = GroupByHocVien;
            }

            model.KH_LOP_KH_MA = id1;
            model.SearchResults = _db.Filter<DKH_LOP>(o => o.KH_LOP_KH_MA == id1);

            return View(model);
        }

        [HttpPost]
        public ActionResult GPS_DKH_LOP(CrudModelDKH_LOP model)
        {
            //thực hiện tạo lớp ở đây
            var sumHocviens = _db.GetTable<DHV>().Where(o => o.HV_KH_MA == model.KH_LOP_KH_MA)
                     .GroupBy(g => g.HV_HANGDT_MA)
                     .Select(r => new
                     {
                         HV_HANGDT_MA = r.Key,
                         XEPLICH_NUMBER_HOCVIEN = r.Count()
                     }).ToDictionary(o => o.HV_HANGDT_MA);// hạng làm key


            if (sumHocviens != null && sumHocviens.Count > 0)
            {
                Dictionary<string, int> GroupByHocVien = new Dictionary<string, int>();

                for (int i = 0; i < sumHocviens.Keys.Count; i++)
                {
                    GroupByHocVien.Add(sumHocviens.Keys.ToList()[i], sumHocviens[sumHocviens.Keys.ToList()[i]].XEPLICH_NUMBER_HOCVIEN);
                }
                model.GroupByHocVien = GroupByHocVien;
            }

            //Thuật toán xếp lớp:
            //Get học vien theo khóa học + hạng 

            if (model.GroupByHocVien != null && model.GroupByHocVien.Count > 0)
            {
                foreach (KeyValuePair<string, int> entry in model.GroupByHocVien)
                {
                    var _MAX_HOCVIEN = model.MAX_HOCVIEN.Value;
                    var _MIN_HOCVIEN = model.MIN_HOCVIEN;
                    var _KH_MA = model.KH_LOP_KH_MA;
                    var _HANGDT_MA = entry.Key;
                    var _COUNT = entry.Value;
                    List<DHV> _lstDHV = _db.Filter<DHV>(o => o.HV_KH_MA == _KH_MA && o.HV_HANGDT_MA == _HANGDT_MA);

                    //chia lớp theo từng hạng:
                    List<List<DHV>> _lstDHV_LOP = COMMON.Helpers.splitList<DHV>(_lstDHV, _MAX_HOCVIEN);

                    //Thực hiện sinh mã lớp và insert vào DB:
                    //1. Insert vào bảng lớp DKH_LOP
                    for (int i = 0; i < _lstDHV_LOP.Count; i++)
                    {
                        //create lớp
                        var _DKH = _db.GetOne<DKH>(o => o.KH_MA == _KH_MA);
                        DKH_LOP _DKH_LOP = new DKH_LOP();
                        _DKH_LOP.KH_LOP_MA = this.CreateDKH_LOP_MA(_KH_MA);
                        _DKH_LOP.KH_LOP_DVGTVT_MA = _DKH.KH_DVGTVT_MA;
                        _DKH_LOP.KH_LOP_KH_MA = _KH_MA;
                        _DKH_LOP.KH_LOP_TEN = _HANGDT_MA + "K" + (i + 1).ToString();
                        _DKH_LOP.KH_LOP_HANGDT_MA = _HANGDT_MA;
                        var _SHANGDT = _db.GetOne<SHANGDT>(o => o.HANGDT_MA == _HANGDT_MA);
                        _DKH_LOP.KH_LOP_HGPLX_MA = _SHANGDT.HANGDT_HGPLX_MA;
                        _DKH_LOP.KH_LOP_NGAYBD = _DKH.KH_NGAYBD;
                        _DKH_LOP.KH_LOP_NGAYKT = _DKH.KH_NGAYKT;
                        _DKH_LOP.KH_LOP_TONGHV = _lstDHV_LOP[i].Count;
                        _DKH_LOP.KH_LOP_ACTIVE = "1";
                        _DKH_LOP.KH_LOP_NGUOINHAP = User.Identity.Name;
                        _DKH_LOP.KH_LOP_NGAYNHAP = DateTime.Now;

                        _db.Insert<DKH_LOP>(_DKH_LOP);

                        //thêm vào để hiển thị ra list
                        if (model.SearchResults == null)
                            model.SearchResults = new List<DKH_LOP>();
                        model.SearchResults.Add(_DKH_LOP);


                        //2. Insert vào bảng DKH_LOP_HV
                        var _lstDHV_HANGDT = _lstDHV_LOP[i];
                        for (int j = 0; j < _lstDHV_HANGDT.Count; j++)
                        {
                            DKH_LOP_HV _DKH_LOP_HV = new DKH_LOP_HV();
                            _DKH_LOP_HV.KH_LOP_HV_KH_LOP_MA = _DKH_LOP.KH_LOP_MA;
                            _DKH_LOP_HV.KH_LOP_HV_DVGTVT_MA = _DKH_LOP.KH_LOP_DVGTVT_MA;
                            _DKH_LOP_HV.KH_LOP_HV_KH_MA = _KH_MA;
                            _DKH_LOP_HV.KH_LOP_HV_HV_MA = _lstDHV_HANGDT[j].HV_MA;
                            _DKH_LOP_HV.KH_LOP_HV_NGUOINHAP = User.Identity.Name;
                            _DKH_LOP_HV.KH_LOP_HV_NGAYNHAP = DateTime.Now;

                            _db.Insert<DKH_LOP_HV>(_DKH_LOP_HV);
                        }



                        //Update trạng thái DA_XEP_LOP =1 vào bảng DHV
                    }
                    //2. Insert vào bảng DKH_LOP_HV

                    //Update trạng thái DA_XEP_LOP =1 vào bảng DHV

                }
            }


            return View(model);
        }


        //Tạo mã lớp học:
        public string CreateDKH_LOP_MA(string KH_MA)
        {
           
            var maxDKH_LOP_MA = _db.Filter<DKH_LOP>(o => o.KH_LOP_KH_MA == KH_MA).OrderByDescending(o => o.KH_LOP_MA).FirstOrDefault();
            string strDKH_LOP_MA = KH_MA + "001";

            if (maxDKH_LOP_MA != null)
            {
                var curDKH_LOP_MA = int.Parse(maxDKH_LOP_MA.KH_LOP_MA.Substring(KH_MA.Length, 3)) + 1;
                strDKH_LOP_MA = KH_MA + (curDKH_LOP_MA < 10 ? "00" + curDKH_LOP_MA.ToString() : (curDKH_LOP_MA < 100 ? "0" + curDKH_LOP_MA.ToString() : curDKH_LOP_MA.ToString()));
            }

            return strDKH_LOP_MA;
        }


        #region "Create schedule Lịch học"
        public ActionResult GPS_DKH_LOP_LH_SCHED(string id1, string id2, string id3, string id4)
        {
            ViewBag.DKH_LOP_MA = id1;
            TempData["DKH_LOP_MA"] = id1;
            TempData["KH_LOP_DVGTVT_MA"] = id2;
            TempData["KH_LOP_KH_MA"] = id3;
            TempData["KH_LOP_HANGDT_MA"] = id4;


            var sched = new DHXScheduler(this);

            sched.Lightbox.Add(new LightboxText("text", "Mô tả"));

            var check = new LightboxCheckbox("highlighting", "Important") { MapTo = "color", CheckedValue = "#FE7510" };

            sched.Lightbox.Add(check);

            var date = new LightboxMiniCalendar("time", "Ngày");
            sched.Lightbox.Add(date);


            var KH_LOP_LH_GV_MAs = _db.Filter<DGV>(o=>o.GV_DVGTVT_MA==id2).Select(
               r => new
               {
                   key = r.GV_MA,
                   label = r.GV_TEN,
               }).OrderBy(r => r.key).ToList();

            var KH_LOP_LH_GV_MASelect = new LightboxSelect("KH_LOP_LH_GV_MA", "Giảng viên");
            KH_LOP_LH_GV_MASelect.AddOptions(KH_LOP_LH_GV_MAs);
            sched.Lightbox.Add(KH_LOP_LH_GV_MASelect);

            var SHTDAOTAOs = _db.GetAll<SHTDAOTAO>().Select(
              r => new
              {
                  key = r.HTDT_MA,
                  label = r.HTDT_MOTA,
              }).OrderBy(r => r.key).ToList();

            var SHTDAOTAOSelect = new LightboxSelect("KH_LOP_LH_HTDT_MA", "Hình thức");
            SHTDAOTAOSelect.AddOption(new LightboxSelectOption("", "--None--"));
            SHTDAOTAOSelect.AddOptions(SHTDAOTAOs);
            sched.Lightbox.Add(SHTDAOTAOSelect);


            //Phương tiện
            var KH_LOP_LH_PT_MAs = _db.Filter<DPT>(o=>o.PT_DVGTVT_MA==id2).Select(
            r => new
            {
                key = r.PT_BIENSO,
                label = r.PT_BIENSO,
            }).OrderBy(r => r.key).ToList();

            var KH_LOP_LH_PT_MASelect = new LightboxSelect("KH_LOP_LH_PT_MA", "Phương tiện");
            KH_LOP_LH_PT_MASelect.AddOption(new LightboxSelectOption("", "--None--"));
            KH_LOP_LH_PT_MASelect.AddOptions(KH_LOP_LH_PT_MAs);
            sched.Lightbox.Add(KH_LOP_LH_PT_MASelect);

            sched.Lightbox.Add(new LightboxText("KH_LOP_LH_SOHOCVIEN", "Số học viên") { Height = 25 });

            sched.LoadData = true;
            sched.EnableDataprocessor = true;

            //allows to postback changes from the server
            sched.UpdateFieldsAfterSave();

            sched.InitialDate = DateTime.Now;

            //
            //List<ViewModelSched_DKH_LOP_LH> lst = new List<ViewModelSched_DKH_LOP_LH>();
            //var DKH_LOP_LHs = _db.Filter<DKH_LOP_LH>(o => o.KH_LOP_LH_KH_LOP_MA == id1.ToString());
            //if (DKH_LOP_LHs != null)
            //{
            //    for (int i = 0; i < DKH_LOP_LHs.Count; i++)
            //    {
            //        var _item = DKH_LOP_LHs[i];
            //        var _itemLH = new ViewModelSched_DKH_LOP_LH();
            //        _itemLH.start_date = _item.KH_LOP_LH_TUNGAY;
            //        _itemLH.end_date = _item.KH_LOP_LH_DENNGAY;
            //        _itemLH.KH_LOP_LH_GV_MA = _item.KH_LOP_LH_GV_MA;
            //        _itemLH.KH_LOP_LH_PT_MA = _item.KH_LOP_LH_PT_MA;
            //        _itemLH.text = _item.KH_LOP_LH_MOTA;
            //        _itemLH.id = int.Parse(_item.KH_LOP_LH_MA);

            //        lst.Add(_itemLH);
            //    }
            //}
            //sched.Data.Parse(lst);

            //  sched.Extensions.Add(SchedulerExtensions.Extension.QuickInfo);
           
            return View(sched);
            //    return View();
        }



        public ContentResult Data()
        {
            var id1 = TempData["DKH_LOP_MA"];
            List<ViewModelSched_DKH_LOP_LH> lst = new List<ViewModelSched_DKH_LOP_LH>();
            var DKH_LOP_LHs = _db.Filter<DKH_LOP_LH>(o => o.KH_LOP_LH_KH_LOP_MA == id1.ToString());
            if (DKH_LOP_LHs != null)
            {
                for (int i = 0; i < DKH_LOP_LHs.Count; i++)
                {
                    var _item = DKH_LOP_LHs[i];
                    var _itemLH = new ViewModelSched_DKH_LOP_LH();
                    _itemLH.start_date = _item.KH_LOP_LH_TUNGAY;
                    _itemLH.end_date = _item.KH_LOP_LH_DENNGAY;
                    _itemLH.KH_LOP_LH_GV_MA = _item.KH_LOP_LH_GV_MA;
                    _itemLH.KH_LOP_LH_PT_MA = _item.KH_LOP_LH_PT_BIENSO;

                    if (_item.KH_LOP_LH_SOHOCVIEN != null)
                        _itemLH.KH_LOP_LH_SOHOCVIEN = _item.KH_LOP_LH_SOHOCVIEN.Value.ToString();
                    if (!string.IsNullOrWhiteSpace(_item.KH_LOP_LH_HTDT_MA))
                        _itemLH.KH_LOP_LH_HTDT_MA = _item.KH_LOP_LH_HTDT_MA;
                    _itemLH.text = _item.KH_LOP_LH_MOTA;
                    _itemLH.id = int.Parse(_item.KH_LOP_LH_MA);

                    lst.Add(_itemLH);
                }
            }

            var data = new SchedulerAjaxData(lst);
            TempData["DKH_LOP_MA"] = id1;
            return (data);
        }

        public ContentResult Save(int? id, FormCollection actionValues)
        {
            var id1 = TempData["DKH_LOP_MA"].ToString();
            var _DKH_LOP = _db.GetOne<DKH_LOP>(o => o.KH_LOP_MA == id1);

            var action = new DataAction(actionValues);
            var changedEvent = (ViewModelSched_DKH_LOP_LH)DHXEventsHelper.Bind(typeof(ViewModelSched_DKH_LOP_LH), actionValues);
            var color = "";
            if (actionValues["color"] == "#FE7510")
            {
                color = "#FE7510";
            }
            else
            {
                if (changedEvent.start_date < DateTime.Now)
                    color = "#ccc";
                else
                    color = "#76B007";
            }

            if (!changedEvent.KH_LOP_LH_SOHOCVIEN.IsNumberic())
            {
                TempData["DKH_LOP_MA"] = id1;
                return Content("Số học viên phải là số", "text/javascript");
            }

            try
            {
                var _lstDKH_LOP_LH = _db.GetAll<DKH_LOP_LH>();
                if (action.Type != DataActionTypes.Delete && IsCollidesWithOthers(changedEvent, _lstDKH_LOP_LH))
                {
                    action.Type = DataActionTypes.Error;
                    action.Message = "This room is already booked for this date.";
                    TempData["DKH_LOP_MA"] = id1;

                    List<ViewModelSched_DKH_LOP_LH> lst = new List<ViewModelSched_DKH_LOP_LH>();
                    var DKH_LOP_LHs = _db.Filter<DKH_LOP_LH>(o => o.KH_LOP_LH_KH_LOP_MA == id1.ToString());
                    if (DKH_LOP_LHs != null)
                    {
                        for (int i = 0; i < DKH_LOP_LHs.Count; i++)
                        {
                            var _item = DKH_LOP_LHs[i];
                            var _itemLH = new ViewModelSched_DKH_LOP_LH();
                            _itemLH.start_date = _item.KH_LOP_LH_TUNGAY;
                            _itemLH.end_date = _item.KH_LOP_LH_DENNGAY;
                            _itemLH.KH_LOP_LH_GV_MA = _item.KH_LOP_LH_GV_MA;
                            _itemLH.KH_LOP_LH_PT_MA = _item.KH_LOP_LH_PT_BIENSO;
                            _itemLH.text = _item.KH_LOP_LH_MOTA;
                            _itemLH.id = int.Parse(_item.KH_LOP_LH_MA);

                            lst.Add(_itemLH);
                        }
                    }

                    var data = new SchedulerAjaxData(lst);
                    TempData["DKH_LOP_MA"] = id1;

                    return Content("Phương tiện và Giảng viên không được trùng lịch", "text/javascript");
                }
                switch (action.Type)
                {

                    case DataActionTypes.Insert:

                        try
                        {
                            var _item = new DKH_LOP_LH();
                            var _idItem = 1;
                            var maxId = _db.GetAll<DKH_LOP_LH>().OrderByDescending(o => o.KH_LOP_LH_MA).FirstOrDefault();
                            if (maxId != null)
                                _idItem = int.Parse(maxId.KH_LOP_LH_MA) + 1;
                            _item.KH_LOP_LH_MA = _idItem.ToString();
                            _item.KH_LOP_LH_MOTA = changedEvent.text;
                            _item.KH_LOP_LH_TUNGAY = changedEvent.start_date;
                            _item.KH_LOP_LH_DENNGAY = changedEvent.end_date;
                            _item.KH_LOP_LH_GV_MA = changedEvent.KH_LOP_LH_GV_MA;
                            _item.KH_LOP_LH_PT_BIENSO = changedEvent.KH_LOP_LH_PT_MA;
                            _item.KH_LOP_LH_NGUOINHAP = User.Identity.Name;
                            _item.KH_LOP_LH_NGAYNHAP = DateTime.Now;
                            _item.KH_LOP_LH_ACTIVE = "1";

                            _item.KH_LOP_LH_KH_LOP_MA = id1;
                            _item.KH_LOP_LH_KH_MA = _DKH_LOP.KH_LOP_KH_MA;
                            _item.KH_LOP_LH_DVGTVT_MA = _DKH_LOP.KH_LOP_DVGTVT_MA;
                            _item.KH_LOP_LH_HTDT_MA = changedEvent.KH_LOP_LH_HTDT_MA;
                            _item.KH_LOP_LH_SOHOCVIEN = decimal.Parse(changedEvent.KH_LOP_LH_SOHOCVIEN);

                            _db.Insert<DKH_LOP_LH>(_item);

                            changedEvent.id = _idItem;
                        }
                        catch (Exception ex)
                        {
                            action.Type = DataActionTypes.Error;
                            action.Message = ex.ToString();
                        }


                        break;
                    case DataActionTypes.Delete:

                        string idDelete = action.SourceId.ToString();
                        var eventToDelete = _db.GetOne<DKH_LOP_LH>(ev => ev.KH_LOP_LH_MA == idDelete);

                        try
                        {

                            _db.Delete<DKH_LOP_LH>(o => o.KH_LOP_LH_MA == eventToDelete.KH_LOP_LH_MA);
                        }
                        catch (Exception ex)
                        {
                            action.Type = DataActionTypes.Error;
                            action.Message = ex.ToString();
                        }

                        break;
                    default:// "update"    
                        string idUpdate = action.SourceId.ToString();
                        var eventToUpdate = _db.GetOne<DKH_LOP_LH>(ev => ev.KH_LOP_LH_MA == idUpdate);
                        //   DHXEventsHelper.Update(eventToUpdate, changedEvent, new List<string>() { "id" });

                        try
                        {

                            eventToUpdate.KH_LOP_LH_MOTA = changedEvent.text;
                            eventToUpdate.KH_LOP_LH_TUNGAY = changedEvent.start_date;
                            eventToUpdate.KH_LOP_LH_DENNGAY = changedEvent.end_date;
                            eventToUpdate.KH_LOP_LH_GV_MA = changedEvent.KH_LOP_LH_GV_MA;
                            eventToUpdate.KH_LOP_LH_PT_BIENSO = changedEvent.KH_LOP_LH_PT_MA;
                            eventToUpdate.KH_LOP_LH_NGUOINHAP = User.Identity.Name;
                            eventToUpdate.KH_LOP_LH_NGAYNHAP = DateTime.Now;
                            eventToUpdate.KH_LOP_LH_ACTIVE = "1";

                            eventToUpdate.KH_LOP_LH_KH_LOP_MA = id1;
                            eventToUpdate.KH_LOP_LH_KH_MA = _DKH_LOP.KH_LOP_KH_MA;
                            eventToUpdate.KH_LOP_LH_DVGTVT_MA = _DKH_LOP.KH_LOP_DVGTVT_MA;
                            eventToUpdate.KH_LOP_LH_HTDT_MA = changedEvent.KH_LOP_LH_HTDT_MA;
                            eventToUpdate.KH_LOP_LH_SOHOCVIEN = decimal.Parse(changedEvent.KH_LOP_LH_SOHOCVIEN);

                            _db.Update<DKH_LOP_LH>(eventToUpdate);
                        }
                        catch (Exception ex)
                        {
                            action.Type = DataActionTypes.Error;
                            action.Message = ex.ToString();
                        }

                        break;
                }

                action.TargetId = changedEvent.id;
            }
            catch (Exception ex)
            {
                action.Type = DataActionTypes.Error;

                List<ViewModelSched_DKH_LOP_LH> lst = new List<ViewModelSched_DKH_LOP_LH>();
                var DKH_LOP_LHs = _db.Filter<DKH_LOP_LH>(o => o.KH_LOP_LH_KH_LOP_MA == id1.ToString());
                if (DKH_LOP_LHs != null)
                {
                    for (int i = 0; i < DKH_LOP_LHs.Count; i++)
                    {
                        var _item = DKH_LOP_LHs[i];
                        var _itemLH = new ViewModelSched_DKH_LOP_LH();
                        _itemLH.start_date = _item.KH_LOP_LH_TUNGAY;
                        _itemLH.end_date = _item.KH_LOP_LH_DENNGAY;
                        _itemLH.KH_LOP_LH_GV_MA = _item.KH_LOP_LH_GV_MA;
                        _itemLH.KH_LOP_LH_PT_MA = _item.KH_LOP_LH_PT_BIENSO;
                        _itemLH.text = _item.KH_LOP_LH_MOTA;
                        _itemLH.id = int.Parse(_item.KH_LOP_LH_MA);

                        lst.Add(_itemLH);
                    }
                }

                var data = new SchedulerAjaxData(lst);
                TempData["DKH_LOP_MA"] = id1;
                return (data);
                // RedirectToAction("GPS_DKH_LOP_LH_SCHED", "GPS_XepLich", new { id1 = id1 });
            }

            //var result = new AjaxSaveResponse(action);
            //result.UpdateField("color", color);//property will be updated on the client
            //return result;

            TempData["DKH_LOP_MA"] = id1;
            //  return (new SchedulerFormResponseScript(action, changedEvent));
            return new AjaxSaveResponse(action);
        }


        private bool IsCollidesWithOthers(ViewModelSched_DKH_LOP_LH booking, List<DKH_LOP_LH> repo)
        {
            if (repo != null)
            {
                var collidedBookings = repo.Where(
                b => b.KH_LOP_LH_MA != booking.id.ToString() && (b.KH_LOP_LH_GV_MA == booking.KH_LOP_LH_GV_MA || b.KH_LOP_LH_PT_BIENSO == booking.KH_LOP_LH_PT_MA)
                    && b.KH_LOP_LH_TUNGAY < booking.end_date && b.KH_LOP_LH_DENNGAY > booking.start_date
                );

                if (collidedBookings.Any()) return true;
            }


            return false;
        }


        #endregion



        #region"Function"
        public void initialCategorySearchAction(ViewModelGPS_Sched_DKH viewModel)
        {
            // ApplicationUser curUser = getCurrentUser();

            ViewBag.DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40"), "DVGTVT_MA", "DVGTVT_TEN");
            //  ViewBag.GV_HANGDT_MAs = new SelectList(_db.GetAll<SHANGDT>(), "HANGDT_MA", "HANGDT_TEN");

        }

        public void initialCategoryCreateAction()
        {
            // ApplicationUser curUser = getCurrentUser();

            ViewBag.GV_DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40"), "DVGTVT_MA", "DVGTVT_TEN");
            ViewBag.GV_HANGDT_MAs = new SelectList(_db.GetAll<SHANGDT>(), "HANGDT_MA", "HANGDT_TEN");
            ViewBag.GV_HGPLX_MAs = new SelectList(_db.GetAll<SHANGGPLX>(), "HGPLX_MA", "HGPLX_TEN");
            ViewBag.GV_NOICT_DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40"), "DVGTVT_MA", "DVGTVT_TEN");
            ViewBag.GV_NOICT_DVHC_MAs = new SelectList(_db.GetAll<SDONVIHC>(), "DVHC_MA", "DVHC_TENDAYDU");
            ViewBag.GV_HTDTAO_MAs = new SelectList(_db.Filter<SHTDAOTAO>(o => o.HTDT_ACTIVE == "1"), "HTDT_MA", "HTDT_MOTA");
            ViewBag.GV_HTTUYENDUNGs = "CT.HĐ";
        }

        public void initialCategoryEditAction(CrudModelDGV view)
        {
            // ApplicationUser curUser = getCurrentUser();

            ViewBag.GV_DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40"), "DVGTVT_MA", "DVGTVT_TEN");
            ViewBag.GV_HANGDT_MAs = new SelectList(_db.GetAll<SHANGDT>(), "HANGDT_MA", "HANGDT_TEN");
            ViewBag.GV_HGPLX_MAs = new SelectList(_db.GetAll<SHANGGPLX>(), "HGPLX_MA", "HGPLX_TEN");
            ViewBag.GV_NOICT_DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40"), "DVGTVT_MA", "DVGTVT_TEN");
            ViewBag.GV_NOICT_DVHC_MAs = new SelectList(_db.GetAll<SDONVIHC>(), "DVHC_MA", "DVHC_TENDAYDU");
            ViewBag.GV_HTDTAO_MAs = new SelectList(_db.Filter<SHTDAOTAO>(o => o.HTDT_ACTIVE == "1"), "HTDT_MA", "HTDT_MOTA");
            ViewBag.GV_HTTUYENDUNGs = "CT.HĐ";
        }


        #endregion


    }


    public class ViewModelSched_DKH_LOP_LH
    {
        public string text { get; set; }
        public DateTime? start_date { get; set; }
        public DateTime? end_date { get; set; }
        public int id { get; set; }
        public string KH_LOP_LH_GV_MA { get; set; }
        public string KH_LOP_LH_PT_MA { get; set; }
        public string KH_LOP_LH_HTDT_MA { get; set; }
        public string KH_LOP_LH_SOHOCVIEN { get; set; }


    }
    public class ColoredEvent
    {

        [StringLength(18)]
        public string color { get; set; }
        public DateTime? end_date { get; set; }
        public int id { get; set; }
        public int? room_id { get; set; }
        public DateTime? start_date { get; set; }
        [Column(TypeName = "text")]
        public string text { get; set; }
    }
}